const { list } = require('../mock')
class BlogController {
    getBlog = async (ctx) => {
        const { pageIndex, pageSize, keyword } = ctx.query;
        let data = list
            .filter(item => item.name.includes(keyword || "")) // includes(undefined) = false 
            .slice((pageIndex - 1) * pageSize, pageIndex * pageSize)
        ctx.body = {
            success: true,
            data,
            total: list.length
        }
    }
    addBlog = async (ctx) => {
        const { name, content } = ctx.request.body;
        list.unshift({ name, content, id: Date.now() })
        ctx.body = list;
    }
    editBlog = async (ctx) => {
        const { name, content, id } = ctx.request.body;
        const ind = list.findIndex(item => item.id == id)
        list[ind].name = name;
        list[ind].content = content;
        ctx.body = list;
    }
    delBlog = async (ctx) => {
        const { id } = ctx.params;
        const ind = list.findIndex(item => item.id == id)
        list.splice(ind, 1)
        ctx.body = list;
    }
}

module.exports = new BlogController()